package com.zyy.dataSource.config.aspect;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mysql.cj.util.StringUtils;
import com.zyy.dataSource.config.DataSourceConfig;
import com.zyy.dataSource.config.DynamicDataSource;
import com.zyy.dataSource.entity.TestDataSourceConfig;
import com.zyy.dataSource.service.TestDataSourceConfigService;
import com.zyy.dataSource.service.TestService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Enumeration;


@Aspect
@Component
@Slf4j
public class DynamicDataSourceAspect {
    @Autowired
    public DynamicDataSource dynamicDataSource;
    @Autowired
    public DataSourceConfig dataSourceConfig;
    @Autowired
    public ApplicationContext context;
    @Autowired
    public TestService service;
    @Autowired
    public TestDataSourceConfigService dataSourceConfigService;
    @Pointcut(value="@annotation(UserDataSource)")
    public void pointcut(){}

    @Before(value = "pointcut()",argNames = "joinPoint")
    public void doBefore(JoinPoint joinPoint) throws NoSuchFieldException, IllegalAccessException {
        String datas = null;
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (attributes != null) {
            HttpServletRequest request = attributes.getRequest();
            datas = request.getHeader("datas");
            // 业务逻辑处理获取到的请求头数据
        }
        System.out.println("前端请求头"+datas);
        if (!StringUtils.isNullOrEmpty(datas)) {
            QueryWrapper<TestDataSourceConfig> wrapper = new QueryWrapper<TestDataSourceConfig>();
            wrapper.eq("id",datas);
            TestDataSourceConfig testDataSourceConfig = dataSourceConfigService.getOne(wrapper);
            log.info(testDataSourceConfig.toString());
            log.info("datas 不为空"+ datas);
            dataSourceConfig.setDataType(testDataSourceConfig.getType());
            dataSourceConfig.setUrl(testDataSourceConfig.getUrl());
            dataSourceConfig.setUsername(testDataSourceConfig.getUsername());
            dataSourceConfig.setPassword(testDataSourceConfig.getPassword());
            dataSourceConfig.setDriverClassName(testDataSourceConfig.getDriver());
            dynamicDataSource.changeDataSource(dataSourceConfig);
        }
        log.info("前置通知");
    }
    @After(value = "pointcut()",argNames = "joinPoint")
    public void doAfter(JoinPoint joinPoint) throws NoSuchFieldException, IllegalAccessException {
        log.info("后置通知");
    }
    @AfterThrowing(value = "pointcut()")
    public void doAfterThrowing(){
        log.info("异常通知");
    }
    @Around(value = "pointcut()", argNames = "pjp")
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
        Object result =null;
        try {
            result= pjp.proceed();
        }finally {
            DynamicDataSource.clearDataSource();
        }
        return result;
    }



}
